<?php
function get_article_list($cid=0,$limit){
	global $_G;
	$cates = $_G['cache']->get('category');
        $limit<1 && $limit = 15;
	$articles = array();
	if(ican('view_hidden_category')) {
		$where = '';
	}else{
		$where = ' AND c.status=0';
	}
	if($cid){
		$where .= ' AND c.cid='.intval($cid);
	}
	if(!ISADMIN){
		$where .=' AND c.access IN(0,'.$_G[groupid].')';
	}
	$sql = "SELECT a.*,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 $where AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.aid DESC ";
	$query = $_G['db']->page_query($sql,$limit);
	$num = $_G['db']->result("SELECT count(*) as num FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 $where AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.aid DESC ");
	while($data = $_G['db']->fetch_array($query)) {
		$articles[] = $data ;
	}
	$pager = pager($num,$limit);
	return array('datas'=>$articles,'page'=>$pager);
}

function get_latest_article($num=10){
	global $_G;
	if(ican('view_hidden_category')) {
		$where = '';
	}else{
		$where = ' AND c.status=0';
	}
	if($cid){
		$where .= ' AND c.cid='.intval($cid);
	}
	if(!ISADMIN){
		$where .=' AND c.access IN(0,'.$_G[groupid].')';
	}
	$sql = "SELECT a.*,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0  $where AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.aid DESC LIMIT ".$num;
	$a =  $_G['db']->fetch_all($sql);
	return $a;
}
function get_hot_article($num=10){
	global $_G;
	if(ican('view_hidden_category')) {
		$status = '1';
	}else{
		$status = 'c.status=0';
	}
	$sql = "SELECT a.*,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND $status AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.views DESC LIMIT ".$num;
	$a =  $_G['db']->fetch_all($sql);
	return $a;
}
function get_rand_article($num=10){
	global $_G;
	if(ican('view_hidden_category')) {
		$status = '1';
	}else{
		$status = 'c.status=0';
	}
	$sql = "SELECT a.*,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND $status AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." AND RAND()<0.5  LIMIT ".$num;
	$a =  $_G['db']->fetch_all($sql);
	return $a;
}
function get_latest_comment($num=10){
	global $_G;
	$query = $_G['db']->query("SELECT c.cid,c.username,c.content,c.referid,a.title FROM ".table('comment')."c LEFT JOIN".table('article')."a ON a.aid=c.referid WHERE c.type=0 AND c.status=1 ORDER BY c.cid DESC limit ".intval($num));
	while($row = $_G['db']->fetch_array($query)) {
		$comments[$row['cid']] = $row;
	}
	return 	$comments;	
}

function get_index_ads(){
	return '';
}